const moment = require('../../../utils/moment.js')
const { get, post } = require('../../../utils/request.js')
Page({

  /**
   * 页面的初始数据 
   * TODO 时间间隔恢复到5分钟，而不是2分钟
   */
  data: {
    templateMsgId: [
      'dmBkJ8mESktdLBHS3OhgFxHKX5WDNJZfepomCGUhjKw'
    ],
    dataId: -1,
    timesArray: ['1次', '2次', '3次'],
    timeUnitArray: [[2, 10, 20, 30], ['分钟', '小时', '天']],
    timeUnitMinutes: [2, 10, 20, 30],
    timeUnitHours: [1, 2, 3, 4, 5],
    timeUnitDays: [1, 2],
    prevTimeUnitArray: [[2, 10, 20, 30, 40], ['分钟', '小时', '天']],
    prevTimeUnitMinutes: [2, 10, 20, 30, 40],
    prevTimeUnitHours: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    prevTimeUnitDays: [1, 2, 3, 4, 5, 6, 7],
    durationTime: '1小时',
    prevDurationTime: '1小时',
    weekArray: [
      { name: '周一', value: 2, shortName: '一' },
      { name: '周二', value: 3, shortName: '二' },
      { name: '周三', value: 4, shortName: '三' },
      { name: '周四', value: 5, shortName: '四' },
      { name: '周五', value: 6, shortName: '五' },
      { name: '周六', value: 7, shortName: '六' },
      { name: '周日', value: 1, shortName: '日' }
    ],
    monthDayArray: [['01月', '02月', '03月', '04月', '05月', '06月', '07月', '08月', '09月', '10月', '11月', '12月'], ['01号', '02号', '03号', '04号', '05号', '06号', '07号', '08号', '09号', '10号', '11号', '12号', '13号', '14号', '15号', '16号', '17号', '18号', '19号', '20号', '21号', '22号', '23号', '24号', '25号', '26号', '27号', '28号', '29号', '30号', '31号']],
    lunarDayArray: [['正月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '冬月', '腊月'], ['初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十', '廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十',]],
    lunarDay: '',
    lunarDayTemp: '',
    lunarOnlyDayArray: ['初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十', '廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十',],
    lunarOnlyDay: '',
    lunarDayIndex: [0, 0],
    monthDay: '',
    dayOfMonth: ['01号', '02号', '03号', '04号', '05号', '06号', '07号', '08号', '09号', '10号', '11号', '12号', '13号', '14号', '15号', '16号', '17号', '18号', '19号', '20号', '21号', '22号', '23号', '24号', '25号', '26号', '27号', '28号', '29号', '30号', '31号'],
    weekIndexs: [],
    weekText: '',
    loadTypeArray: ['房贷', '车贷', '装修贷', '信用贷', '抵押贷', '经营贷'],
    loadTypeIndex: 0,
    taskTypeArray: ['生日', '纪念日', '信用卡', '汽车保养', '食品到期'],
    taskTypeIndex: '0',
    dateTypeArray: ['公历', '农历'],
    dateTypeIndex: '0',
    remindTypeArray: ['一次性', '每天', '每周', '每月', '每年'],
    remindTypeArraySolar: ['一次性', '每天', '每周', '每月', '每年'],
    remindTypeArrayLunar: ['一次性', '每年'],
    remindTypeIndex: 0,
    remindDateOfMonth: '',
    dayOfMonthIndex: '',
    remindDate: '',
    targetTime: '',
    remindName: '',
    timesCycle: '0',
    prevTimesCycle: '0',
    remindWay: '',
    remindCycle: '',
    remark: '',
    endDate: '',
    startDate: '',
    isPrevTip: false,
    isMoreTip: false,
    isMoreEndWay: false,
    isSetEndDate: false,
    isActive: true,
    isContinueKeep: false,
    isTopShow: false,
    afterEndWay: false,
    nextExecuteCount: 5,
    executeTimes: [],
    mensesLastDateOne: '',
    mensesLastDateTwo: '',
    mensesKeepDateNum: '6',
    isMensesStartDateAlter: true,
    isMensesStartDateAlter1: true,
    isMensesStartDateAlter2: true,
    isOvulationDateAlter: false,
    isOvulationDateAlter1: true,
    isOvulationDateAlter2: true,
    isOvulationDurationAlter: false,
    isOvulationDurationAlter1: true,
    isOvulationDurationAlter2: true,
    isSafeDateAlter: false,
    isSafeDateAlter1: true,
    isSafeDateAlter2: true,
    isShowNormalInput: true,
    mensesDayTime: '07:00',
    mensesPrevDayTime: '07:00',
    ovulationDayTime: '07:00',
    ovulationPrevDayTime: '07:00',
    ovulationDurationDayTime: '07:00',
    ovulationDurationPrevDayTime: '07:00',
    safeDateDayTime: '07:00',
    safeDatePrevDayTime: '07:00',
    calenders: [],
    mensesStartDate: '',
    mensesEndDate: '',
    startTipDate: '',
    loadMoneyPerMonth: '',
    isNewUser: false,
    relationArray: [],
    relationIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad({ id, copy, remindType }) {
    const unicode = wx.getStorageSync('unicode');
    this.setData({
      mensesStartDate: moment().add(-3, 'month').format('YYYY-MM-DD'),
      mensesEndDate: moment().format('YYYY-MM-DD'),
      startTipDate: moment().format('YYYY-MM-DD'),
    })
    get('remind/form/initdata', null, ({ lunarDate, relations, isNewUser }) => {
      this.setData({
        lunarDay: lunarDate,
        lunarDayTemp: lunarDate,
        relationArray: relations,
        isNewUser: isNewUser
      })
    })

    get('remindtype/list', {
      'filter_unicode_eq_s': unicode,
      'filter_isActive_eq_i': 1,
      'orderBy': 'sort'
    }, (data) => {
      if (data && data.length > 0) {
        const taskTypeArray = data.map(item => item.remindTypeName);
        let remindTypeIndex = 0;
        let remindDateStr = moment().add(1, 'day').format('YYYY-MM-DD');
        if (taskTypeArray[0] == '贷款还款') {
          remindTypeIndex = 3;
        } else if (taskTypeArray[0] == '生日' || taskTypeArray[0] == '纪念日') {
          remindTypeIndex = 4;
          remindDateStr = moment().add(1, 'day').format('MM月DD日');
        }
        this.setData({
          taskTypeArray: taskTypeArray,
          remindTypeIndex: remindTypeIndex,
          mensesLastDateOne: moment().format('YYYY-MM-DD'),
          mensesLastDateTwo: moment().add(-29, 'day').format('YYYY-MM-DD'),
          remindDate: remindDateStr,
          targetTime: moment().format('HH:mm'),
          dayOfMonthIndex: moment().format('DD') - 1
        })

        if (id) {
          if (remindType == '生理期') {
            get(`menses/get/${id}`, null, (data) => {
              this.setData({
                dataId: copy ? -1 : id,
                remindName: data.remindName,
                taskTypeIndex: this.data.taskTypeArray.indexOf('生理期'),
                mensesLastDateOne: data.mensesLastDateOneStr,
                mensesLastDateTwo: data.mensesLastDateTwoStr,
                mensesKeepDateNum: data.mensesKeepDateNum,
                isMensesStartDateAlter: data.isMensesStartDateAlter,
                isMensesStartDateAlter1: data.isMensesStartDateAlter1,
                isMensesStartDateAlter2: data.isMensesStartDateAlter2,
                isOvulationDateAlter: data.isOvulationDateAlter,
                isOvulationDateAlter1: data.isOvulationDateAlter1,
                isOvulationDateAlter2: data.isOvulationDateAlter2,
                isOvulationDurationAlter: data.isOvulationDurationAlter,
                isOvulationDurationAlter1: data.isOvulationDurationAlter1,
                isOvulationDurationAlter2: data.isOvulationDurationAlter2,
                isSafeDateAlter: data.isSafeDateAlter,
                isSafeDateAlter1: data.isSafeDateAlter1,
                isSafeDateAlter2: data.isSafeDateAlter2,
                isShowNormalInput: data.isShowNormalInput,
                mensesDayTime: data.mensesDayTime,
                mensesPrevDayTime: data.mensesPrevDayTime,
                ovulationDayTime: data.ovulationDayTime,
                ovulationPrevDayTime: data.ovulationPrevDayTime,
                ovulationDurationDayTime: data.ovulationDurationDayTime,
                ovulationDurationPrevDayTime: data.ovulationDurationPrevDayTime,
                safeDateDayTime: data.safeDateDayTime,
                safeDatePrevDayTime: data.safeDatePrevDayTime,
                isTopShow: data.isTopShow,
                isActive: data.isActive
              })
              this.freshMensesCalenderData();
            })
          } else {
            get(`remind/get/${id}`, { isInit: true }, (data) => {
              const weeks = data.targetWeek.split(',');
              const remindDate = moment(data.remindDate).format('YYYY-MM-DD');
              const day = remindDate.split('-')[2];
              const taskTypeIndex = this.data.taskTypeArray.indexOf(data.remindTypeName);
              const dateTypeIndex = this.data.dateTypeArray.indexOf(data.dateType);
              let remindTypeIndex = '';
              if (data.remindCycleWay == '每年') {
                const temp = moment(data.remindDate).format('MM-DD').split('-');
                data.remindDate = `${temp[0]}月${temp[1]}号`
              } else {
                data.remindDate = moment(data.remindDate).format('YYYY-MM-DD');
              }

              if (data.remindTypeName == '贷款还款') {
                remindTypeIndex = 3;
              } else if (data.remindTypeName == '生日' || data.remindTypeName == '纪念日') {
                remindTypeIndex = 4;
              } else {
                remindTypeIndex = this.data.remindTypeArray.indexOf(data.remindCycleWay);
              }

              const timesIndex = this.data.timesArray.indexOf(data.timesCycle);
              const prevTimesIndex = this.data.timesArray.indexOf(data.prevTimesCycle);
              const loadTypeIndex = this.data.loadTypeArray.indexOf(data.loadTypeName);
              const relationshipIndex = this.data.relationArray.indexOf(data.relationship);

              this.data.weekArray.forEach(item => {
                item.checked = weeks.some(p => p == item.value)
              })
              const dayIndex = data.lunarDayIndex.split(',');
              const lunarDay = this.data.lunarDayArray[0][dayIndex[0]] + this.data.lunarDayArray[1][dayIndex[1]];
              this.setData({
                dataId: copy ? -1 : id,
                remindName: copy ? '' : data.remindName,
                remark: copy ? '' : data.remark,
                taskTypeIndex: taskTypeIndex,
                dateTypeIndex: dateTypeIndex,
                remindTypeIndex: remindTypeIndex,
                startDate: data.startDate && data.startDate != 'null' ? moment(data.startDate).format('YYYY-MM-DD') : '',
                endDate: data.endDate && data.endDate != 'null' ? moment(data.endDate).format('YYYY-MM-DD') : '',
                remindDate: data.remindDate,
                targetTime: data.remindTime,
                targetWeek: data.targetWeek,
                isMoreTip: data.isMoreTip,
                isSetEndDate: data.isSetEndDate,
                isPrevTip: data.isPrevTip,
                isActive: data.isActive,
                isContinueKeep: data.isContinueKeep,
                isTopShow: data.isTopShow,
                timesCycle: timesIndex,
                prevTimesCycle: prevTimesIndex,
                loadTypeIndex: loadTypeIndex,
                lunarDayIndex: data.lunarDayIndex.split(','),
                lunarDay: lunarDay,
                dayOfMonthIndex: parseInt(day) - 1,
                durationTime: data.durationTime,
                prevDurationTime: data.prevDurationTime,
                remindWay: data.remindWay,
                weekIndexs: weeks,
                weekArray: this.data.weekArray,
                afterEndWay: data.autoDealWay == 'disabled',
                loadMoneyPerMonth: data.loadMoneyPerMonth,
                relationIndex: relationshipIndex
              })
              this.freshNextFiveTime(true)
            })
          }
        } else {
          if (taskTypeArray[0] == '生理期') {
            this.freshMensesCalenderData();
          } else {
            this.freshNextFiveTime(true);
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  bindTaskTypeChange({ detail: { value } }) {
    let remindTypeIndex = 0;
    const temp = this.data.taskTypeArray[value];
    let remindDateStr = moment().add(1, 'day').format('YYYY-MM-DD');
    if (temp == '贷款还款') {
      remindTypeIndex = 3;
    } else if (temp == '生日' || temp == '纪念日') {
      remindTypeIndex = 4;
      remindDateStr = moment().add(1, 'day').format('MM月DD日');
    } else {
      remindTypeIndex = 0;
    }

    this.setData({
      taskTypeIndex: value,
      durationTime: '1小时',
      prevDurationTime: '1小时',
      lunarDay: this.data.lunarDayTemp,
      lunarOnlyDay: '',
      lunarDayIndex: [0, 0],
      monthDay: '',
      weekIndexs: [],
      weekText: '',
      loadTypeIndex: 0,
      dateTypeIndex: '0',
      remindTypeIndex: remindTypeIndex,
      remindDateOfMonth: '',
      dayOfMonthIndex: moment().format('DD') - 1,
      remindDate: remindDateStr,
      targetTime: moment().format('HH:mm'),
      remindName: '',
      timesCycle: '0',
      prevTimesCycle: '0',
      remindWay: '',
      remindCycle: '',
      remark: '',
      endDate: '',
      startDate: '',
      isPrevTip: false,
      isMoreTip: false,
      isMoreEndWay: false,
      isSetEndDate: false,
      isActive: true,
      isContinueKeep: false,
      isTopShow: false,
      afterEndWay: false,
      mensesLastDateOne: moment().format('YYYY-MM-DD'),
      mensesLastDateTwo: moment().add(-29, 'day').format('YYYY-MM-DD'),
      mensesKeepDateNum: '6',
      isMensesStartDateAlter: true,
      isMensesStartDateAlter1: true,
      isMensesStartDateAlter2: true,
      isOvulationDateAlter: false,
      isOvulationDateAlter1: true,
      isOvulationDateAlter2: true,
      isOvulationDurationAlter: false,
      isOvulationDurationAlter1: true,
      isOvulationDurationAlter2: true,
      isSafeDateAlter: false,
      isSafeDateAlter1: true,
      isSafeDateAlter2: true,
      isShowNormalInput: true,
      mensesDayTime: '07:00',
      mensesPrevDayTime: '07:00',
      ovulationDayTime: '07:00',
      ovulationPrevDayTime: '07:00',
      ovulationDurationDayTime: '07:00',
      ovulationDurationPrevDayTime: '07:00',
      safeDateDayTime: '07:00',
      safeDatePrevDayTime: '07:00',
      loadMoneyPerMonth: '',
      relationIndex: 0
    })

    if (temp == '生理期') {
      this.freshMensesCalenderData();
    } else {
      this.freshNextFiveTime();
    }
  },
  bindRelationshipChange({ detail: { value } }) {
    this.setData({
      relationIndex: value
    })
  },
  bindDateTypeChange({ detail: { value } }) {
    this.setData({
      dateTypeIndex: value
    })
    this.freshNextFiveTime()
  },
  bindRemindTypeChange({ detail: { value } }) {
    if (value == 0 || value == 3 || value == 4) {
      this.setData({
        timeUnitArray: [[5, 10, 20, 30], ['分钟', '小时', '天']]
      })
    } else if (value == 1 || value == 2) {
      this.setData({
        timeUnitArray: [[5, 10, 20, 30], ['分钟', '小时']]
      })
    }

    if (value == 4) {
      const month = this.data.monthDayArray[0][moment().format('MM') - 1];
      const day = this.data.monthDayArray[1][moment().format('DD') - 1];
      this.setData({
        remindDate: `${month}${day}`
      });
    } else if (value == 3) {
      const day = this.data.dayOfMonth[this.data.dayOfMonthIndex].replace('号', '');
      this.setData({
        remindDate: `1990-01-${day}`
      })
    } else {
      this.setData({
        remindDate: moment().format('YYYY-MM-DD'),
      });
    }

    this.setData({
      remindTypeIndex: value
    })

    if (value != 2) {
      this.freshNextFiveTime()
    }

  },

  bindWeekChange({ detail: { value } }) {
    let t = '';

    if (value.length == 5 && value[4] == '周五') {
      t = '工作日'
    } else if (value.length == 2 && value[0] == '周六' && value[1] == '周日') {
      t = '周末'
    } else if (value.length == 7) {
      t = '每天'
    } else {
      t = value.map(item => {
        if (item == 0) {
          return '周一'
        } else if (item == 1) {
          return '周二'
        } else if (item == 2) {
          return '周三'
        } else if (item == 3) {
          return '周四'
        } else if (item == 4) {
          return '周五'
        } else if (item == 5) {
          return '周六'
        } else if (item == 6) {
          return '周日'
        }
      }).join(',');
    }
    this.setData({
      weekText: t
    })
  },
  monthDayValueChange({ detail: { value } }) {
    this.setData({
      remindDate: this.data.monthDayArray[0][value[0]] + this.data.monthDayArray[1][value[1]]
    })
    this.freshNextFiveTime()
  },
  moreTipChange({ detail: { value } }) {
    this.setData({
      isMoreTip: value
    })
    this.freshNextFiveTime();
  },
  prevTipChange({ detail: { value } }) {
    this.setData({
      isPrevTip: value
    })
    this.freshNextFiveTime();
  },
  endDateChange({ detail: { value } }) {
    this.setData({
      isSetEndDate: value
    })
    this.freshNextFiveTime();
  },
  onColumnChange({ detail: { value } }) {
    timeUnitArray
    this.setData({
      durationTime: this.data.timeUnitArray[0][value[0]] + this.data.timeUnitArray[1][value[1]]
    })
  },
  prevCycleTipValueChange({ detail: { value } }) {
    this.setData({
      prevDurationTime: this.data.prevTimeUnitArray[0][value[0]] + this.data.prevTimeUnitArray[1][value[1]]
    })
    this.freshNextFiveTime()
  },
  cycleTipValueChange({ detail: { value } }) {
    this.setData({
      durationTime: this.data.timeUnitArray[0][value[0]] + this.data.timeUnitArray[1][value[1]]
    })
    this.freshNextFiveTime()
  },
  bindRemindDateChange({ detail: { value } }) {
    const date = moment(value, 'YYYY-MM-DD').format('YYYY年MM月DD日')
    this.setData({
      remindDate: date
    })
  },
  getSubmitData() {
    const weeks = this.data.weekIndexs.join(',');
    const startDate = this.data.startDate == '' ? '' : moment(this.data.startDate, 'YYYY-MM-DD');
    const endDate = this.data.endDate == '' ? '' : moment(this.data.endDate, 'YYYY-MM-DD');
    let remindDate;
    if (this.data.remindDate.indexOf('月') > -1) {
      const date = '1990-' + this.data.remindDate.replace('月', '-').replace('日', '');
      remindDate = moment(date, 'YYYY-MM-DD')
    } else {
      remindDate = moment(this.data.remindDate, 'YYYY-MM-DD')
    }
    const params = {
      remindName: this.data.remindName,
      remindTypeName: this.data.taskTypeArray[this.data.taskTypeIndex],
      dateType: this.data.dateTypeArray[this.data.dateTypeIndex],
      remindCycleWay: this.data.remindTypeArray[this.data.remindTypeIndex],
      remindDate: remindDate,
      remindTime: this.data.targetTime,
      targetWeek: weeks,
      lunarDayIndex: this.data.lunarDayIndex.join(','),
      isSetEndDate: this.data.isSetEndDate,
      endDate: endDate,
      startDate: startDate,
      isMoreTip: this.data.isMoreTip,
      isPrevTip: this.data.isPrevTip,
      isActive: this.data.isActive,
      isContinueKeep: this.data.isContinueKeep,
      isTopShow: this.data.isTopShow,
      autoDealWay: this.data.afterEndWay ? 'disabled' : 'destroy',
      timesCycle: this.data.timesArray[this.data.timesCycle],
      prevTimesCycle: this.data.timesArray[this.data.prevTimesCycle],
      loadTypeName: this.data.loadTypeArray[this.data.loadTypeIndex],
      durationTime: this.data.durationTime,
      prevDurationTime: this.data.prevDurationTime,
      remindWay: '微信服务号',
      remark: this.data.remark,
      loadMoneyPerMonth: this.data.loadMoneyPerMonth,
      relationship: this.data.relationArray[this.data.relationIndex]
    }

    if (this.data.dataId > 0) {
      params.id = this.data.dataId
    }
    return params;
  },
  submitData() {
    if (this.data.remindName.trim() == '') {
      let remindNameMsg;
      const taskType = this.data.taskTypeArray[this.data.taskTypeIndex];
      if (taskType == '生理期') {
        remindNameMsg = '请填写姓名'
      } else if (taskType == '生日') {
        remindNameMsg = '请填写名字'
      } else if (taskType == '贷款还款') {
        remindNameMsg = '请填写贷款项名'
      } else if (taskType == '汽车保养') {
        remindNameMsg = '请填写保养项目'
      } else if (taskType == '食品到期') {
        remindNameMsg = '请填写食品名称'
      } else {
        remindNameMsg = '请填写事项标题'
      }
      wx.showToast({
        title: remindNameMsg,
        icon: 'error',
        duration: 2000
      })
      return;
    }
    if (this.data.isActive && this.data.nextExecuteCount == 0) {
      wx.showToast({
        title: '没有执行计划',
        icon: 'error',
        duration: 2000
      })
      return;
    }

    if (this.data.taskTypeArray[this.data.taskTypeIndex] == '生理期') {
      this.submitMensesData();
    } else {
      this.submitRemindTask();
    }

  },
  bindPrevMultiPickerColumnChange({ detail: { column, value } }) {
    if (column == 1) {
      if (value == 0) {
        this.data.prevTimeUnitArray[0] = this.data.prevTimeUnitMinutes;
      } else if (value == 1) {
        this.data.prevTimeUnitArray[0] = this.data.prevTimeUnitHours;
      } if (value == 2) {
        this.data.prevTimeUnitArray[0] = this.data.prevTimeUnitDays;
      }

      this.setData({
        prevTimeUnitArray: this.data.prevTimeUnitArray
      })
    }
  },
  bindMultiPickerColumnChange({ detail: { column, value } }) {
    if (column == 1) {
      if (value == 0) {
        this.data.timeUnitArray[0] = this.data.timeUnitMinutes;
      } else if (value == 1) {
        this.data.timeUnitArray[0] = this.data.timeUnitHours;
      } if (value == 2) {
        this.data.timeUnitArray[0] = this.data.timeUnitDays;
      }

      this.setData({
        timeUnitArray: this.data.timeUnitArray
      })
    }
  },
  onChangeWeekValue({ detail: { value } }) {
    this.setData({
      weekIndexs: value
    })
    if (value.length > 0) {
      this.freshNextFiveTime()
    }

  },
  freshNextFiveTime(isInit) {
    const params = this.getSubmitData();
    if (isInit && isInit instanceof Boolean) {
      params.isInit = isInit;
    }
    get('remind/nexttime', params, (data) => {
      this.setData({
        executeTimes: data,
        nextExecuteCount: data.length
      })
    })
  },
  onChangeDayOfMonth() {
    const day = this.data.dayOfMonth[this.data.dayOfMonthIndex].replace('号', '');
    this.setData({
      remindDate: `1990-01-${day}`
    })
    this.freshNextFiveTime()
  },
  onChangeLunarDay({ detail: { value } }) {
    const date = this.data.lunarDayArray[0][value[0]] + this.data.lunarDayArray[1][value[1]];
    this.setData({
      lunarDayIndex: value,
      lunarDay: date
    })
    this.freshNextFiveTime()
  },
  activeChange({ detail: { value } }) {
    this.setData({
      isActive: value
    })

    if (this.data.taskTypeArray[this.data.taskTypeIndex] == '生理期') {
      this.freshMensesCalenderData();
    } else {
      this.freshNextFiveTime();
    }

  },
  topShowChange({ detail: { value } }) {
    this.setData({
      isTopShow: value
    })
  },
  isMoreEndWayChange({ detail: { value } }) {
    this.setData({
      isMoreEndWay: value
    })
  },
  onClickEndWay() {
    this.setData({
      afterEndWay: !this.data.afterEndWay
    })
  },
  tipTimeChange({ detail: { value }, currentTarget: { dataset: { mensestype } } }) {
    const types = mensestype.split('-');
    if (types[0] == '1') {
      if (types[1] == '1') {
        this.setData({
          mensesDayTime: value
        })
      } else if (types[1] == '2') {
        this.setData({
          mensesPrevDayTime: value
        })
      }
    } else if (types[0] == '2') {
      if (types[1] == '1') {
        this.setData({
          ovulationDayTime: value
        })
      } else if (types[1] == '2') {
        this.setData({
          ovulationPrevDayTime: value
        })
      }
    } else if (types[0] == '3') {
      if (types[1] == '1') {
        this.setData({
          ovulationDurationDayTime: value
        })
      } else if (types[1] == '2') {
        this.setData({
          ovulationDurationPrevDayTime: value
        })
      }
    } else if (types[0] == '4') {
      if (types[1] == '1') {
        this.setData({
          safeDateDayTime: value
        })
      } else if (types[1] == '2') {
        this.setData({
          safeDatePrevDayTime: value
        })
      }
    }
    this.freshMensesCalenderData();
  },
  mensesSwitchChange({ detail: { value }, currentTarget: { dataset: { mensestype } } }) {
    const types = mensestype.split('-');
    if (types[0] == '1') {
      if (types[1] == '0') {
        this.setData({
          isMensesStartDateAlter: value
        })
      } else if (types[1] == '1') {
        this.setData({
          isMensesStartDateAlter1: value
        })
      } else if (types[1] == '2') {
        this.setData({
          isMensesStartDateAlter2: value
        })
      }

    } else if (types[0] == '2') {
      if (types[1] == '0') {
        this.setData({
          isOvulationDateAlter: value
        })
      } else if (types[1] == '1') {
        this.setData({
          isOvulationDateAlter1: value
        })
      } else if (types[1] == '2') {
        this.setData({
          isOvulationDateAlter2: value
        })
      }

    } else if (types[0] == '3') {
      if (types[1] == '0') {
        this.setData({
          isOvulationDurationAlter: value
        })
      } else if (types[1] == '1') {
        this.setData({
          isOvulationDurationAlter1: value
        })
      } else if (types[1] == '2') {
        this.setData({
          isOvulationDurationAlter2: value
        })
      }
    } else if (types[0] == '4') {
      if (types[1] == '0') {
        this.setData({
          isSafeDateAlter: value
        })
      } else if (types[1] == '1') {
        this.setData({
          isSafeDateAlter1: value
        })
      } else if (types[1] == '2') {
        this.setData({
          isSafeDateAlter2: value
        })
      }
    }
    this.freshMensesCalenderData();
  },
  freshMensesCalenderData(value, index) {
    const params = this.getMensesParams();
    if (index && value) {
      if (index == 1) {
        params.mensesLastDateOne = moment(value)
      } else {
        params.mensesLastDateTwo = moment(value)
      }
    }

    get('menses/calendar', params, ({ nextTimes, mensesCalenders }) => {
      this.setData({
        calenders: mensesCalenders,
        currentCalenderIndex: 0,
        executeTimes: nextTimes,
        nextExecuteCount: nextTimes.length
      })
    })
  },
  freshMensesCalender({ detail: { value }, currentTarget: { dataset: { index } } }) {

    if (index == 1) {
      if (moment(value).isBefore(moment(this.data.mensesLastDateTwo))) {
        let newDate2 = moment(value).add(-29, 'day').format('YYYY-MM-DD')
        if (moment(newDate2).isBefore(this.data.mensesStartDate)) {
          newDate2 = this.data.mensesStartDate
        }
        this.setData({
          mensesLastDateTwo: newDate2
        })
      }
    } else {
      if (moment(value).isAfter(moment(this.data.mensesLastDateOne))) {
        let newDate1 = moment(value).add(29, 'day').format('YYYY-MM-DD')
        if (moment(newDate1).isAfter(this.data.mensesEndDate)) {
          newDate1 = this.data.mensesEndDate
        }
        this.setData({
          mensesLastDateOne: newDate1
        })
      }
    }

    this.freshMensesCalenderData(value, index);

  },
  getMensesParams() {
    const params = {
      remindName: this.data.remindName,
      remark: this.data.remark,
      mensesLastDateOne: moment(this.data.mensesLastDateOne),
      mensesLastDateTwo: moment(this.data.mensesLastDateTwo),
      mensesKeepDateNum: this.data.mensesKeepDateNum,
      isMensesStartDateAlter: this.data.isMensesStartDateAlter,
      isMensesStartDateAlter1: this.data.isMensesStartDateAlter1,
      isMensesStartDateAlter2: this.data.isMensesStartDateAlter2,
      isOvulationDateAlter: this.data.isOvulationDateAlter,
      isOvulationDateAlter1: this.data.isOvulationDateAlter1,
      isOvulationDateAlter2: this.data.isOvulationDateAlter2,
      isOvulationDurationAlter: this.data.isOvulationDurationAlter,
      isOvulationDurationAlter1: this.data.isOvulationDurationAlter1,
      isOvulationDurationAlter2: this.data.isOvulationDurationAlter2,
      isSafeDateAlter: this.data.isSafeDateAlter,
      isSafeDateAlter1: this.data.isSafeDateAlter1,
      isSafeDateAlter2: this.data.isSafeDateAlter2,
      mensesDayTime: this.data.mensesDayTime,
      mensesPrevDayTime: this.data.mensesPrevDayTime,
      ovulationDayTime: this.data.ovulationDayTime,
      ovulationPrevDayTime: this.data.ovulationPrevDayTime,
      ovulationDurationDayTime: this.data.ovulationDurationDayTime,
      ovulationDurationPrevDayTime: this.data.ovulationDurationPrevDayTime,
      safeDateDayTime: this.data.safeDateDayTime,
      safeDatePrevDayTime: this.data.safeDatePrevDayTime,
      isActive: this.data.isActive,
      isTopShow: this.data.isTopShow
    }

    if (this.data.dataId > 0) {
      params.id = this.data.dataId
    }
    return params;
  },
  submitMensesData() {
    const templateMsgId = this.data.templateMsgId;
    const requestMensesTask = this.requestMensesTask.bind(this)
    //TODO 如果一次性订阅会自动取消，这里要每次检查setting
    // requestMensesTask();
    if (this.data.isNewUser) {
      wx.requestSubscribeMessage({
        tmplIds: templateMsgId,
        success() {
          requestMensesTask();
          post('login/increase')
        },
        fail() {
          requestMensesTask();
        }
      })
    } else {
      requestMensesTask();
    }
  },

  requestMensesTask() {
    const params = this.getMensesParams();
    get('menses/save', params, () => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000
      })
      setTimeout(() => {
        wx.reLaunch({
          url: '/pages/remind/remind',
        })
      }, 2000);
    })
  },

  submitRemindTask() {
    if (this.data.isSetEndDate && (this.data.startDate == '' || this.data.endDate == '')) {
      wx.showToast({
        title: '请填写起止日期',
        icon: 'error',
        duration: 2000
      })
      return;
    }

    const remindType = this.data.remindTypeArray[this.data.remindTypeIndex];
    const weekIndex = this.data.weekIndexs;

    if (remindType == '每周' && weekIndex.length == 0) {
      wx.showToast({
        title: '请选择周几',
        icon: 'error',
        duration: 2000
      })
      return;
    }

    // this.requestRemindTask();

    const templateMsgId = this.data.templateMsgId;
    const requestRemindTask = this.requestRemindTask.bind(this)
    if (this.data.isNewUser) {
      wx.requestSubscribeMessage({
        tmplIds: templateMsgId,
        success() {
          requestRemindTask();
          post('login/increase')
        },
        fail() {
          requestRemindTask();
        }
      })
    } else {
      requestRemindTask();
    }
  },
  requestRemindTask() {
    const params = this.getSubmitData();
    post('remind/createNewRemindTask', params, () => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000
      })
      setTimeout(() => {
        wx.reLaunch({
          url: '/pages/remind/remind',
        })
      }, 2000);
    })
  },
  changeMemerialDay({ detail: { value } }) {
    this.setData({
      remindDate: this.data.monthDayArray[0][value[0]] + this.data.monthDayArray[1][value[1]]
    })

    this.freshNextFiveTime();
  },





})